package yxy.game.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import com.alibaba.fastjson.JSONObject;

import yxy.apple.http.HTTPConnection;
import yxy.apple.http.request.JSONRequest;
import yxy.apple.http.response.JSONResponse;

public class HttpUtil {

	public static JSONObject response(String url, int timeout, JSONObject body) throws Exception {
		HTTPConnection connection = null;
		try {
			connection = new HTTPConnection(timeout);
			JSONRequest jsonRequest = new JSONRequest(url);
			jsonRequest.setHeader("Content-Type", "application/json");
			jsonRequest.setBody(body);
			JSONResponse response = connection.request(jsonRequest, JSONResponse.class);
			
			JSONObject result = response.body();
			return result;
		} catch (Exception e) {
			throw e;
		} finally {
			if (connection != null) {
				connection.close();
			}
		}
	}
	
//	public static JSONObject httpsresponse(String url, int timeout, JSONObject body) throws Exception {
//		HTTPSConnection connection = null;
//		try {
//			connection = new HTTPSConnection(30000);
//			
////			connection = new HTTPConnection(timeout);
//			JSONRequest jsonRequest = new JSONRequest(url);
//			jsonRequest.setHeader("Content-Type", "application/json");
//			jsonRequest.setBody(body);
//			JSONResponse response = connection.request(jsonRequest, JSONResponse.class);
//			
//			JSONObject result = response.body();
//			return result;
//		} catch (Exception e) {
//			throw e;
//		} finally {
//			if (connection != null) {
//				connection.close();
//			}
//		}
//	}
	
	public static String getMD5(String str) {
		try {
			// 生成一个MD5加密计算摘要
			MessageDigest md = MessageDigest.getInstance("MD5");
			// 进行utf-8编码
			byte[] bs = str.getBytes("utf-8");
			// 计算md5函数
			md.update(bs);
			// digest()最后确定返回md5 hash值，返回值为8为字符串。因为md5 hash值是16位的hex值，实际上就是8位的字符
			// BigInteger函数则将8位的字符串转换成16位hex值，用字符串来表示；得到字符串形式的hash值
			// return new BigInteger(1, md.digest()).toString(16);
			byte[] md5sum = md.digest();
			return toHex(md5sum);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static String toHex(byte[] data) {
		char[] HEX_DIGITS = "0123456789ABCDEF".toCharArray();
		char[] chars = new char[data.length * 2];
		for (int i = 0; i < data.length; i++) {
			chars[i * 2] = HEX_DIGITS[(data[i] >> 4) & 0xf];
			chars[i * 2 + 1] = HEX_DIGITS[data[i] & 0xf];
		}
		return new String(chars);
	}

	public static String getURLEncoderString(String str) {
		String result = "";
		if (null == str) {
			return "";
		}
		try {
			result = java.net.URLEncoder.encode(str, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return result;
	}

	public static String getSignData(JSONObject parseobj, String nosignkey) {
		// params为要参与签名的参数键值对.
		StringBuffer content = new StringBuffer();
		// 按照key做排序

		Set<String> parseset = parseobj.keySet();
		List<String> list = sortSet(parseset);

		for (int i = 0; i < list.size(); i++) {
			String key = list.get(i);
			if ("sign".equals(key))
				continue;
			if (nosignkey != null) {
				if (nosignkey.equals(key)) {
					continue;
				}
			}
			String value = parseobj.getString(key);
			if (value != null) {
				content.append((i == 0 ? "" : "&") + key + "=" + value);
			}
		}
		return content.toString();
	}

	public static List<String> sortSet(Set<String> set) {
		List<String> strlist2 = new ArrayList<>();
		strlist2.addAll(set);
		Collections.sort(strlist2);

		return strlist2;
	}
	
}
